<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\goods\controller;

use cmf\controller\HomeBaseController;
use app\goods\model\GoodsCategoryModel;

class ListController extends HomeBaseController
{
    public function index()
    {
        $id = input('id', 0, 'intval');

        $goodsCategoryModel = new GoodsCategoryModel();
        $category            = $goodsCategoryModel->where('id', $id)->find();
       
        $listTpl = empty($category['list_tpl']) ? 'list' : $category['list_tpl'];

        $this->assign('cat_id', $id);
        $this->assign('category', $category);

        $request = request();
        if( $request->isAjax() ){
            $html = $this->fetch($listTpl.'_ajax');
            
            $return = array(
                // 'error'     => 0,
                'data'      => $html,
                // 'start'     => $start,
                // 'size'      => $size,
                // 'more_data' => empty($more_data) ? 0 : 1 ,
            );
            return $return;
        }
        return $this->fetch('/' . $listTpl);
    }

    //ajax加载更多，查询该分类及其所有子分类下的文章
    public function listAjaxByCatId(){
        
        $cid      = input('cid',0,'intval');
        $start    = input('start',0,'intval');
        $size     = input('size',0,'intval');
        $list_tpl = input('list_tpl','','trim');
        $recomm   = input('recomm',0,'intval');

        $where = array();
        if( $recomm ){
            $where['recommended'] = 1;
        }

        $limit = $start.','.$size;
        $lists = cmf_sql_goods_bycatid($cid,"field:goods_name,goods.goods_id,goods.more,category_id,published_time,post_excerpt;".TMPLGSO."limit:$limit;",$where);

            
        if( count($lists) < 1 ){
            $return = array(
                'error'   => 1,
            );
            return $return;
        }


        $this->assign('lists',$lists);

        //使用的模版
        $html = $this->fetch($list_tpl.'_ajax');

        //是否存在更多数据
        $more_limit = $start+$size.',1';
        $more_data = cmf_sql_goods_bycatid($cid,TMPLGSO."limit:$more_limit;",$where);

        
        $return = array(
            'error'     => 0,
            'data'      => $html,
            'start'     => $start,
            'size'      => $size,
            'more_data' => (count($more_data) < 1) ? 0 : 1 ,
        );
        return $return;
    }

    //ajax加载更多，查询该分类下的文章
    public function listAjax(){
        $cid      = input('cid',0,'intval');
        $start    = input('start',0,'intval');
        $size     = input('size',0,'intval');
        $list_tpl = input('list_tpl','','trim');

        $limit = $start.','.$size;
        $lists = cmf_sql_posts("category_ids:$cid;field:post_id,category_id,post_title,post_source,post_excerpt,post.more,file_url,published_time;limit:$limit;".TMPLGSO);
        // pre($lists,1);

        //没有数据
        if( count($lists) < 1 ){
            $return = array(
                'error'   => 1,
            );
            return $return;
        }
        
        if( !empty($list_tpl) ){
            //赋值，使用的模版
            $this->assign('lists',$lists);
            $html = $this->fetch($list_tpl.'_ajax');
        }

        //是否存在更多数据
        $more_limit = $start+$size.',1';
        $more_data = cmf_sql_posts("category_ids:$cid;field:post_id;limit:$more_limit;".TMPLGSO);

        $return = array(
            'error'     => 0,
            'data'      => empty($list_tpl) ? $lists : $html ,
            'start'     => $start,
            'size'      => $size,
            'more_data' => (count($more_data) < 1) ? 0 : 1 ,
        );
        return $return;
    }

}
